<?php
/**
 * Created by PhpStorm.
 * User: caoyixian
 * Date: 2017/5/24
 * Time: 11:24
 */
namespace App\Libs\WeChat\Pay;

class WxCallBack{


    public function verify($xml){
        $status = false;
        $base = new WxPayDataBase();
        $parsetoarray = $base->FromXml($xml);
        //var_dump($parse);
        $sign = $base->CreateSign($parsetoarray);
        if($parsetoarray['result_code']=='SUCCESS' && $parsetoarray['sign']==$sign) {
            $status = true;
        }

        $reply = new WxPayNotifyReply();
        if($status == true){
            $reply->SetReturn_code("SUCCESS");
            $reply->SetReturn_msg("OK");
            //自主处理逻辑
            //$this->curlpost($url,$parsetoarray);
        } else {
            $reply->SetReturn_code("FAIL");
            $reply->SetReturn_msg("SIGN ERROR");
        }
       //把结果通知微信充值服务器
        return $reply->ToXml();


    }






    /**
     * 模拟实现post调用
     * @param unknown $url
     * @param unknown $data
     */
    private function curlpost($url, $data) {
        $process = curl_init ( $url );
        curl_setopt ( $process, CURLOPT_HEADER, 0 );
        curl_setopt ( $process, CURLOPT_TIMEOUT, 120 );
        curl_setopt($process, CURLOPT_CONNECTTIMEOUT, 120);
        if ( is_array($data) ) {
            foreach( $data as $key => $d ) {
                $param[] = $key."=".$d;
            }
            $params = implode('&', $param);
            curl_setopt ( $process, CURLOPT_POSTFIELDS, $params );
        }
        else {
            curl_setopt ( $process, CURLOPT_POSTFIELDS, $data );
        }
        curl_setopt ( $process, CURLOPT_RETURNTRANSFER, 1 );
        curl_setopt ( $process, CURLOPT_FOLLOWLOCATION, 1 );
        curl_setopt ( $process, CURLOPT_POST, 1 );
        set_time_limit(120);
        $return = curl_exec ( $process );
        curl_close ( $process );
        //需要监控处理结果，便于控制对微信支付接口的回应
        return $return;
    }







}